package com.cdyx.ServiceHospApplication.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdyx.common.exception.YyghException;
import com.cdyx.common.result.Result;
import com.cdyx.model.hosp.HospitalSet;
import com.cdyx.ServiceHospApplication.service.HospitalSetService;
import com.cdyx.service.utils.MD5;
import com.cdyx.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Random;

@Api("医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin//这个注解允许跨域访问
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //1.查询医院设置表所有信息
    @ApiOperation("获取所有医院设置管理")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
       try {
           int i=1/0;
       }catch (Exception e){
           throw new YyghException("失败",201);
       }
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //2.逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        System.out.println("id:"+id);
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.ok();
        }
    }
/*
        @RequestBody 表示按着JSON格式传输数据 提交方式为@PostMapping 这样才能得到JSON格式的数据
         @RequestBody(required = false)中required = false表示可以不传入map
 */
    //3.条件查询带分页
    @PostMapping("findPageHosSet/{current}/{limit}")
    public Result findPageHospSet(
            @PathVariable long current,
            @PathVariable long limit,
           @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo
            //创建page对象，传递当前页，每页记录数
    ) {
        Page<HospitalSet> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        return Result.ok(pageHospitalSet);
    }
    //4.批量删除
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchDelete")
    public Result removeBatchHospSet(@RequestBody List<Long> ids) {
        boolean remove = hospitalSetService.removeByIds(ids);
        if (remove)
            return Result.ok();
        else
            return Result.fail();
    }
         //添加医院设置
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态
        hospitalSet.setStatus(1);
        //签名设置
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
        //根据id获取医院设置
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    //修改医院设置
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //批量删除医院设置
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //医院设置的锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lock(@PathVariable("id") String id,@PathVariable("status") Integer status){
        HospitalSet hosp = hospitalSetService.getById(id);
        hosp.setStatus(status);
        hospitalSetService.updateById(hosp);
        return Result.ok();
    }
    //发送签名的密钥
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable long id){
        HospitalSet hosp = hospitalSetService.getById(id);
        String signKey = hosp.getSignKey();
        String hoscode = hosp.getHoscode();
        //TODO
        return Result.ok();
    }
}
